package com.shangrila.common.html;

import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.shangrila.common.exceptions.GeneralException;
import com.shangrila.common.exceptions.IORuntimeException;
import com.shangrila.common.lang.Assert;

/**
 * 基于htmlparser的html的解析
 * 
 * @author kalman03
 */
public class Jhtml {

	private final String url;
	private final Parser parser;

	// 设置连接的超时时间
	private int connectTimeout;
	// 设置读取数据的超时时间
	private int readTimeout;

	public Jhtml(String url) {
		this(url, 3000, 3000);
	}

	public Jhtml(String url, int connectTimeout, int readTimeout) {
		Assert.assertTrue(StringUtils.isNotBlank(url), "url is required.");
		this.url = url;
		this.connectTimeout = connectTimeout;
		this.readTimeout = readTimeout;
		this.parser = init();
	}

	private Parser init() {
		Parser p = null;
		try {
			URLConnection connection = initConnection();
			p = new Parser(connection);
		} catch (Exception e) {
			throw new IORuntimeException(e);
		}
		return p;
	}

	private URLConnection initConnection() throws Exception {
		URL u = new URL(this.url);
		URLConnection con = u.openConnection();
		con.setAllowUserInteraction(false);
		con.setConnectTimeout(connectTimeout);
		con.setReadTimeout(readTimeout);
		return con;
	}

	public List<String> getLinkList() throws GeneralException {
		return getMatchedLinkList(null);
	}

	public List<String> getMatchedLinkList(String regex)
			throws GeneralException {
		List<String> list = new ArrayList<String>();
		// 通过过滤器过滤出<A>标签
		NodeList nodeList = null;
		try {
			nodeList = parser.extractAllNodesThatMatch(new NodeFilter() {
				private static final long serialVersionUID = 1L;

				// 实现该方法,用以过滤标签
				public boolean accept(Node node) {
					if (node instanceof LinkTag) // <A>标记
						return true;
					return false;
				}
			});
		} catch (ParserException e) {
			throw new GeneralException(e);
		}
		// 打印
		for (int i = 0; i < nodeList.size(); i++) {
			LinkTag n = (LinkTag) nodeList.elementAt(i);
			String link = n.extractLink();
			if (StringUtils.isNotBlank(link)) {
				if (StringUtils.isNotBlank(regex)) {
					if (link.matches(regex))
						list.add(link);
				} else {
					list.add(link);
				}
			}
		}
		return list;
	}

	public static void main(String[] args) {
		try {
			Jhtml jhtml = new Jhtml("http://blog.12xiaoshi.com/favorite");
			List<String> list = jhtml.getLinkList();
			for (String str : list) {
				System.out.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
